//给vue添加一个原型
Vue.prototype.$http=axios;
//给每个异步请求添加默认前缀
axios.defaults.baseURL = 'http://api.360msj.xyz';
//上传图片前缀ip
Vue.prototype.$fast="http://121.37.194.36";

//1 使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //携带token
    let uToken =  localStorage.getItem("token");
    if(uToken){
        config.headers['U-TOKEN']=uToken;
    }
    return config;
},error => {
    Promise.reject(error);
})
//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
    //debugger
    let data = result.data;
    //如果返回的状态为1 ，说明redis过期，清空前台存储的token和loginUser
    // if (data.state === 1){
    //     localStorage.removeItem("token");
    //     localStorage.removeItem("loginUser");
    // }

    if(!data.success && data.msg==="noUser")
        location.href = "/login.html";
    return result;
},error => {
    Promise.reject(error);
})

//获取url中"?"符后的字串
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}